import requests
import bs4
import ast
def get_url(word):
    url = 'https://quark.sm.cn/api/rest?method=tools.fanyi&format=tpl&from=quark&is_dlpage=1&q={}&ft=en2zh&callback=jsonp1'.format(word)
    headers = {
        'referer': 'https://quark.sm.cn/api/rest?method=quark_fanyi.dlpage&uc_param_str=dnntnwvepffrgibijbprsvpidicheiutds&from=kkframenew&schema=v2&format=html&entry=fanyi_jinshan',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
    }
    res = requests.get(url,headers=headers)
    #字符串转字典
    res_dic = ast.literal_eval(res.text.replace('jsonp1(','').replace(');',''))

    return res_dic['tpl']

def find_translation(res):
    new_res = res.replace('\\','')
    soup = bs4.BeautifulSoup(new_res,'html.parser')
    translation = soup.find_all('span',class_="js-c-paragraph-text")

    return translation


def main1(word):


    #word = input('请输入单词:')
    res = get_url(word)
    return find_translation(res)

if __name__ == '__main__':
    main1()